package com.dreamteam.schooling.timetable;

import com.dreamteam.schooling.R;
import com.dreamteam.schooling.R.dimen;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class TimeTableLayout extends ViewGroup {
	private Paint paint;
	public TimeTableLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		paint = new Paint();
		paint.setColor(Color.GRAY);
		paint.setStrokeWidth(2);
		paint.setStrokeCap(Cap.ROUND);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
		int parentWidthMeasureSpec = 0;
		int parentHeightMeasureSpec = 0;
		View leftChild = getChildAt(0);
		LayoutParams params = leftChild.getLayoutParams();
		params.width = parentWidth / 5;
		leftChild.setLayoutParams(params);

//		parentWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
//				parentWidth / 5, 
//				MeasureSpec.EXACTLY);
//
//		parentHeightMeasureSpec = heightMeasureSpec;
		measureChild(leftChild, widthMeasureSpec, heightMeasureSpec);

		for(int i=1; i < getChildCount(); i++) {
			View child =  this.getChildAt(i);
			parentWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
					parentWidth *(4/5), 
					MeasureSpec.EXACTLY);

			int childHeight = R.dimen.timeitem_height;
			parentHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
					childHeight, 
					MeasureSpec.EXACTLY);
			measureChild(child, parentWidthMeasureSpec, parentHeightMeasureSpec);
		}

		setMeasuredDimension(parentWidth, leftChild.getMeasuredHeight());
	}

	/*
	private int calChildHeight(View child) {
		TimeItemView tv = (TimeItemView)child;
		tv.getSize();
		return 0;
	}*/

	@Override
	protected void onLayout(boolean flag, int left, int top, int right, int bottom) {
		// TODO Auto-generated method stub
		//this.getChildCount();
		View leftView = this.getChildAt(0);
		leftView.layout(left, top, left + leftView.getMeasuredWidth(), bottom);

//		TimeItemView tv =  (TimeItemView)this.getChildAt(1);
//		//TextView tv = (TextView)this.getChildAt(1);
//		System.out.println("tv.getText() = " + tv.getText());
//		System.out.println("getChildCount() = " + getChildCount());
		for(int i = 1; i < getChildCount(); i++) {
			
			TimetableItemView tv =  (TimetableItemView)this.getChildAt(i);
			top = tv.getStartTime() * tv.getSize();
			bottom = tv.getEndTime() * tv.getSize();
			
			System.out.println("getStartTime() : " + tv.getStartTime());
			System.out.println("getEndTime() : " + tv.getEndTime());
			System.out.println("R.dimen.timeitem_height : " + R.dimen.timeitem_height + " R.dimen.timeitem_height : " +R.dimen.timeitem_height);
			System.out.println("TimeItem => left : " + leftView.getMeasuredWidth() + left + " top : " + top + " right : " + right + " bottom : " + bottom);
			tv.layout(leftView.getMeasuredWidth() + left, top, right, bottom);
			
		}
	}

	@Override
	protected void dispatchDraw(Canvas canvas) {
		// TODO Auto-generated method stub

		ViewGroup leftView = (ViewGroup)this.getChildAt(0);
		int height = leftView.getChildAt(0).getHeight();
		int childCount = leftView.getChildCount();
		for(int i = 0; i < childCount; i++) {
			canvas.drawLine(0, i * height, this.getWidth(), i * height, paint);
		}

		super.dispatchDraw(canvas);
	}
}
